#pragma once
// MySQL版本
#include "../comm/util.hpp"
#include "../comm/log.hpp"
#include "mysql/mysql.h"

#include <unordered_map>
#include <vector>
#include <cassert>
#include <fstream>
#include <algorithm>

namespace ns_model
{
    using namespace std;
    using namespace ns_util;
    using namespace ns_log;

    struct Question
    {
        string number; // 题号
        string title;  // 名称
        string star;   // 难度: 简单 中等 困难
        string desc;   // 题目描述
        string header; // 用户提交代码
        string tail;   // 测试代码
        int cpu_limit; // 时间要求(S)
        int mem_limit; // 空间要求(KB)
    };

    const string oj_questions = "questions";
    const string host = "127.0.0.1";
    const string user = "oj_client";
    const string password = "123456";
    const string db = "oj";
    const int port = 3306;

    class Model
    {
    private:
        unordered_map<string, Question> questions;

    public:
        Model()
        {
        }
        bool QueryMySql(const string &sql, vector<Question> *out)
        {
            // 创建mysql句柄
            MYSQL *my = mysql_init(nullptr);

            // 连接数据库
            if (nullptr == mysql_real_connect(my, host.c_str(), user.c_str(), password.c_str(), db.c_str(), port, nullptr, 0))
            {
                LOG(FATAL) << "数据库连接失败！" << mysql_error(my) << endl;
                mysql_close(my);
                return false;
            }

            // 设置链接编码格式
            mysql_set_character_set(my, "utf8mb4");
            LOG(INFO) << "数据库连接成功" << endl;

            // 执行sql语句
            if (0 != mysql_query(my, sql.c_str()))
            {
                LOG(WARRING) << sql << " execute error!" << endl;
                return false;
            }

            // 获取结果集
            MYSQL_RES *res = mysql_store_result(my);

            int rows = mysql_num_rows(res);   // 行
            int cols = mysql_num_fields(res); // 列

            Question q;

            for (int i = 0; i < rows; i++)
            {
                MYSQL_ROW row = mysql_fetch_row(res);
                q.number = row[0];
                q.title = row[1];
                q.star = row[2];
                q.desc = row[3];
                q.header = row[4];
                q.tail = row[5];
                q.cpu_limit = atoi(row[6]);
                q.mem_limit = atoi(row[7]);

                out->push_back(q);
            }

            // 释放结果空间
            mysql_free_result(res);
            // 关闭MySQL连接
            mysql_close(my);

            return true;
        }

        bool GetAllQuestion(vector<Question> *out)
        {
            string sql = "select * from ";
            sql += oj_questions;
            return QueryMySql(sql, out);
        }
        bool GetOneQuestion(const string &number, Question *q)
        {
            bool res = false;
            string sql = "select * from ";
            sql += oj_questions;
            sql += " where id = ";
            sql += number;
            vector<Question> result;
            if (QueryMySql(sql, &result))
            {
                if (result.size() == 1)
                {
                    *q = result[0];
                    res = true;
                }
            }
            return res;
        }
    };
}
